# -*- coding: utf-8 -*-
import logging
import os
from typing import Literal

LogLevel = Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']


class Logger:
    level_relations = {
        'DEBUG': logging.DEBUG,
        'INFO': logging.INFO,
        'WARNING': logging.WARNING,
        'ERROR': logging.ERROR,
        'CRITICAL': logging.CRITICAL
    }

    def __init__(self, root_path: str, log_name: str, level: LogLevel = 'INFO',
                 fmt: str = '%(asctime)s - %(levelname)s: %(message)s'):
        self.root_path = root_path
        self.log_name = log_name
        self.fmt = fmt
        self.logger = logging.getLogger(log_name)
        self.logger.setLevel(self.level_relations.get(level))

    def get_logger(self, log_dir: str = 'log', log_suffix: str = '.log'):
        """
        ## get_logger\n
        Create logger object to write log file
        :param log_dir: Directory log file stored in, Default: `log`
        :param log_suffix: Log file suffix, Default: `.log`
        :return: Logger object
        """
        path = os.path.join(self.root_path, log_dir)
        os.makedirs(path, exist_ok=True)
        file_name = os.path.join(path, self.log_name + log_suffix)
        rotate_handler = logging.FileHandler(file_name, encoding='utf-8', mode='a')
        formatter = logging.Formatter(self.fmt)
        rotate_handler.setFormatter(formatter)
        self.logger.addHandler(rotate_handler)
        return self.logger
